//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2011 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------


  /**
   * check if the scroll has reached the bottom
   */
  function reachBottom() {

	    var scrollTop = 0; 
	    var clientHeight = 0; 
	    var scrollHeight = 0;

	    if (document.documentElement && document.documentElement.scrollTop) {
	        scrollTop = document.documentElement.scrollTop;
	    } else if (document.body) {
	        scrollTop = document.body.scrollTop;
	    }
	    
	    if (document.body.clientHeight && document.documentElement.clientHeight) { 
	        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
	    } else {
	        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
		}

	    scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);

	    if (scrollTop + clientHeight == scrollHeight) { 
	        return true; 
	    } else { 
	        return false; 
	    }
   }
  
var reg1 = /^[^&;$%@.\'\"\<>+,]+$/;
var reg2 = /^\S+$/;
function validateChar(reg,objID,type,div,divSuc,divFail,txtSuc,txtFail){
	if(reg==''){
		reg=reg1;
	}
	var obj = document.getElementById(objID);
	if(reg.test(obj.value)){
		if(type=='div'){
			if(div.length>0){
				for(var i = 0;i<div.length;i++){
					document.getElementById(div[i]).style.display="none";
				}
			}
			document.getElementById(divFail).style.display="none";
			document.getElementById(divSuc).style.display="";
		}else {
			alert(txtSuc);
		}
	}else{
		if(type=='div'){
			if(div.length>0){
				for(var i = 0;i<div.length;i++){
					document.getElementById(div[i]).style.display="none";
				}
			}
			document.getElementById(divSuc).style.display="none";
			document.getElementById(divFail).style.display="";
		}else {
			alert(txtFail);
		}
	}
}
function validateCharSimple(objID){
	var obj = document.getElementById(objID);
	if((reg1.test(obj.value))&&(reg2.test(obj.value))){
		return true;
	}else{
		return false;
	}
}

function validateRegistraion(obj,matchObj){
	if(obj.length>0){
		for(var i = 0; i < obj.length; i++){
			if(document.getElementById(obj[i]).value!=''){
			if(matchObj[i]=='zipCode'){
				var reg = /^[0-9]+$/;
				if(reg.test(document.getElementById(obj[i]).value)){
					if(i==obj.length-1){
						return true;
					}
				}else{
					document.getElementById(obj[i]).focus();
					return false;
				}
			}else{
				if(validateCharSimple(obj[i])){
					if(i==obj.length-1){
						return true;
					}
				}else{
					document.getElementById(obj[i]).focus();
					return false;
				}
			}
		}
		}
	}else{
		return true;
	}
}

function validateCharSimpleByValue(objValue){
	if((reg1.test(objValue))&&(reg2.test(objValue))){
		return true;
	}else{
		return false;
	}
}

/**
 * added by liang
 * This function help to get the dynamic data hold in the innerHTML of a div with object definition's format
 * @divId
 */
function getObjectFromDiv(divId){
	  var resultJASONText = dojo.byId(divId);
	  try{
	      if (resultJASONText) {
	      	var resultInfo = eval('('+resultJASONText.innerHTML+')');
	      	
	        if (resultInfo != null && resultInfo !=undefined){
	        	
	        	return resultInfo;
	        } else {
	        	return null;
	        }
	      }
	  }catch (e) {
		  //alert(e);
		  return null;
	  }
}
/**
 * added by liang
 * This function help to get the dynamic data hold in a object definition's format string
 * @objectString
 */
function getObjectFromString(objectString){
	  var resultJASONText = objectString;
	  try{
	      if (resultJASONText != '') {
	      	var resultInfo = eval('('+resultJASONText+')');
	        if (resultInfo != null && resultInfo !=undefined){
	        	return resultInfo;
	        } else {
	        	return null;
	        }
	      }
	  }catch (e) {
		  return null;
	  }
}

/**
 * added by liang
 * This function help to get CustomParams hold in the innerHTML of a div with object definition's format
 * @divId
 */
function getCustomParamsFromDiv(divId){
	var data =  getObjectFromDiv(divId);
	if (data != null) {
 
		return data.getCustomParams();
	} else {
		return null;
	}
     
}
/**
 * added by liang
 * This function help to get CustomParams hold in a object definition's format string
 * @objectString
 */
function getCustomParamsFromString(objectString){
	var data =  getObjectFromString(objectString);
	if (data != null) {
		return data.getCustomParams();
	} else {
		return null;
	}
 
}

/**
 * added by liang
 * This function is used to hide the image border  of an element.
 * @param {DomNode} element The node whose border is to be hidden.
 */
   function changeImageBorder(element, className, ifFotus){
		//element.style.backgroundImage='none';
	   if (!element) return;
	   try{ 
			if(className!="") {
				element.className=className;
			} else { 
				if (ifFotus) {
					if(element.style.border) {
						//element.className="home_image2_borderClass1";
						element.style.borderColor="#eeeeee";
					}
				} else {
					if(element.style.border) {
						//element.className="home_image2_borderClass1";
						element.style.borderColor="#feb771";
					}
				}
			}
		} catch (e) {
			
		} 
		 
   }
   
   function showCustomServ() {
	   window.open('http://chat.looyu.com/chat/chat/p.do?g=39167&c=39415&f=87911','','width=650,height=400,toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=yes, status=no')
   }
   
   /**
    * show hidden image
    */
   function lazyloadForHidePart(container) {
       container.find('img').each(function () {
           var original = $(this).attr("original");
           if (original) {
               $(this).attr('src', original).removeAttr('original');
           }
       });
   }
   
   /**
    * open the url in new window
    * @param url
    * @return
    */
  function openUrlInNewWin(url) {
  	  var encodeUrl = encodeURI(url);
  	  //alert(encodeUrl);
  	  window.open(encodeUrl, "");
  }
    
    /**
     * correctly handle PNG transparency in Win IE 5.5 & 6.
     */
  function correctPNG()  
  {
      var arVersion = navigator.appVersion.split("MSIE"); 
      var version = parseFloat(arVersion[1]); 
      if ((version >= 5.5) && (document.body.filters)) 
      {
         for(var j=0; j<document.images.length; j++) 
         { 
            var img = document.images[j]; 
            var imgName = img.src.toUpperCase(); 
            if (imgName.substring(imgName.length-3, imgName.length) == "PNG") 
            {
               var imgID = (img.id) ? "id='" + img.id + "' " : ""; 
               var imgClass = (img.className) ? "class='" + img.className + "' " : ""; 
               var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "; 
               var imgStyle = "display:inline-block;" + img.style.cssText; 
               if (img.align == "left") imgStyle = "float:left;" + imgStyle; 
               if (img.align == "right") imgStyle = "float:right;" + imgStyle; 
               if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle; 
               var strNewHTML = "<span " + imgID + imgClass + imgTitle 
               + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" 
               + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" 
               + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
               img.outerHTML = strNewHTML ;
               j = j-1 ;
            }
         } 
      }    
  }
  
  /**
   * place the element in to the target container
   * @param elementId
   * @param targetElementId
   * @return
   */
  function placeElementInContainer(elementId, targetElementId) {
	  if (!elementId || elementId == "") {
		  return ;
	  }
	  if (!targetElementId || targetElementId == "") {
		  return ;
	  }
	  
	  var src = document.getElementById(elementId);
	  var container = document.getElementById(targetElementId);
	  if (!src || !container) {
		  return ;
	  }
	  //container.style.position="absolute";
	  var tPosTop = container.offsetTop;
	  var tPosLeft = container.offsetLeft;
	  var tWidth = container.offsetWidth;
	  var tHeight = container.offsetHeight; 
	  var eWidth = src.offsetWidth;
	  var eHeight = src.offsetHeight; 
	  //alert('tPosTop='+ tPosTop);
	  //alert('tPosLeft='+tPosLeft);	  
	  eWidth = tWidth>eWidth? tWidth: eWidth;
	  var ePosTop = tPosTop + Math.abs(tHeight/2-eHeight/2);
	  var ePosLeft = tPosLeft + Math.abs(tWidth/2-eWidth/2);
	  //alert('ePosTop='+ ePosTop);
	  //alert('ePosLeft='+ePosLeft);
	  //re set element pos:
	  
	  src.style.width=eWidth + "px";
	  src.style.display="block";
	  //src.style.position="absolute";
	  src.style.top=ePosTop+"px";
	  src.style.left=ePosLeft+"px"
	  //container.style.position="";
	  //src.style.position="";
  }
	/**
	*  add the url to my favorite foaders
	*/
	function addFavory(title, url){
 
		var url = url==""? location.url : "http://m.gd.ct10000.com";
			//url.substring(0, url.indexOf('.com')+4);
 		try {
			window.external.AddFavorite(url,title); 
		} catch (e) {}
	}